import { heartbeat } from "@/swagger/api/monitor";

let heartbeatTimer: any = null;

export function startHeartbeat() {
  // 避免重复定时
  if (heartbeatTimer) return;

  // 立即发送一次心跳
  heartbeat().catch(e => console.warn("Heartbeat failed:", e));

  // 每 60 秒发送一次心跳
  heartbeatTimer = setInterval(async () => {
    try {
      await heartbeat();
    } catch (e) {
      console.warn("Heartbeat failed:", e);
    }
  }, 60 * 1000);
}

export function stopHeartbeat() {
  if (heartbeatTimer) {
    clearInterval(heartbeatTimer);
    heartbeatTimer = null;
  }
}